﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RazakiBLL;

namespace RazakiSite.UserControls.New
{
    public partial class UCNewsActions : BaseUserControl
    {
        private const string _NEW_TAG = "NewTagId";

        UowRazaki _uowRazaki = new UowRazaki();

        #region Properties

        public short NewTagFirstParentId
        {
            get
            {
                string uniqueKey = (Page as BasePage).UniqueKey;

                return UtilitiesUI.SessionGet<short>(_NEW_TAG + uniqueKey);
            }
            set
            {
                string uniqueKey = (Page as BasePage).UniqueKey;

                UtilitiesUI.SessionSet(_NEW_TAG + uniqueKey, value);
            }
        }

        public int NewId
        {
            get { return UtilitiesUI.GetQueryString<int>("newid", false); }
        }

        public bool ShowBackToList { get; set; }

        public bool ShowDelete { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigControls();
        }

        private void ConfigControls()
        {
            BtnBackToList.Visible = ShowBackToList;
            BtnDelete.Visible = ShowDelete;

            BtnDelete.OnClientClick = string.Format(UtilitiesUI.Sentences(ESentences.confirmJs), "حذف خبر");
        }

        protected void Btn_Click(object sender, EventArgs e)
        {
            Button linkButton = (Button)sender;
            string url = string.Empty;
            string queryString = string.Empty;

            switch (linkButton.CommandName.ToLower())
            {
                case "backtolist":
                        queryString = UtilitiesUI.SetQueryString(new Dictionary<string, string>
                                                                 {
                                                                     {"parentid", NewTagFirstParentId.ToString()}
                                                                 });
                        url = string.Format("~/PagesNews/NewsList.aspx{0}", queryString);

                    break;

                case "delete":
                    _uowRazaki.NewRepository.RemoveById(NewId);
                    _uowRazaki.Save();

                    queryString = UtilitiesUI.SetQueryString(new Dictionary<string, string>
                                                                 {
                                                                     {"parentid", NewTagFirstParentId.ToString()}
                                                                 });
                    url = string.Format("~/PagesNews/NewsList.aspx{0}", queryString);

                    break;
            }

            if (!string.IsNullOrWhiteSpace(url))
                Response.Redirect(url);
        }

    }
}